package com.example.sso.sso_client;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.sso.message.SaSsoMessage;
import cn.dev33.satoken.sso.processor.SaSsoClientProcessor;
import cn.dev33.satoken.sso.template.SaSsoClientTemplate;
import cn.dev33.satoken.sso.template.SaSsoClientUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Sa-Token-SSO Client端 Controller 
 */

@Slf4j
// @RestController
public class SsoClientController {

    // 首页 
    @RequestMapping("/")
    public String index() {
        String str = "<h2>Sa-Token SSO-Client 应用端 (模式三)</h2>" +
                "<p>当前会话是否登录：" + StpUtil.isLogin() + " (" + StpUtil.getLoginId("") + ")</p>" +
                "<p> " +
                "<a href='/sso/login?back=/'>登录</a> - " +
                "<a href='/sso/logoutByAlone?back=/'>单应用注销</a> - " +
                "<a href='/sso/logout?back=self'>全端注销</a> - " +
                "<a href='/sso/myInfo' target='_blank'>账号资料</a>" +
                "</p>";
        return str;
    }

    
     /** SSO-Client端：处理所有SSO相关请求
     *         http://{host}:{port}/sso/login            -- Client 端登录地址
     *         http://{host}:{port}/sso/logout            -- Client 端注销地址（isSlo=true时打开）
     *         http://{host}:{port}/sso/pushC            -- Client 端接收消息推送地址
     */

    // @RequestMapping("/sso/*")
    public Object ssoRequest() {
        log.info("ssoRequest");
        Object res = SaSsoClientProcessor.instance.dister();
        log.info("ssoRequest:{}", res);
        return res;
    }

    // SSO-Client：登录地址
    @RequestMapping("/sso/login")
    public Object ssoLogin() {
        return SaSsoClientProcessor.instance.ssoLogin();
    }

    // SSO-Client：单点注销地址
    @RequestMapping("/sso/logout")
    public Object ssoLogout() {
        return SaSsoClientProcessor.instance.ssoLogout();
    }

    // SSO-Client：单点注销回调
    @RequestMapping("/sso/logoutCall")
    public Object ssoLogoutCall() {
        return SaSsoClientProcessor.instance.ssoLogoutCall();
    }

    // SSO-Client：接收消息推送地址
    @RequestMapping("/sso/ssoPushC")
    public Object ssoPushC() {
        return SaSsoClientProcessor.instance.ssoPushC();
    }
    

    // 配置SSO相关参数
    @Autowired
    private void configSso(SaSsoClientTemplate ssoClientTemplate) {

    }

    // 当前应用独自注销 (不退出其它应用)
    @RequestMapping("/sso/logoutByAlone")
    public Object logoutByAlone() {
        StpUtil.logout();
        return SaSsoClientProcessor.instance._ssoLogoutBack(SaHolder.getRequest(), SaHolder.getResponse());
    }

    // 查询我的账号信息：sso-client 前端 -> sso-center 后端 -> sso-server 后端
    @RequestMapping("/sso/myInfo")
    public Object myInfo() {
        // 如果尚未登录
        if( ! StpUtil.isLogin()) {
            return "尚未登录，无法获取";
        }

        // 获取本地 loginId
        Object loginId = StpUtil.getLoginId();

        // 构建消息对象 
        SaSsoMessage message = new SaSsoMessage();
        message.setType("userinfo");
        message.set("loginId", loginId);

        // 推送至 sso-server，并接收响应数据 
        SaResult result = SaSsoClientUtil.pushMessageAsSaResult(message);

        // 返回给前端
        return result;
    }

}

